package cn.gjxblog.mq.mechanism.returns;

import cn.gjxblog.mq.utils.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;


/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/7/4 11:48
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        Connection connection = ConnectionUtils.getConnection();

        Channel channel = connection.createChannel();


        //5 通过channel发送数据
        String exchangeName = "test_return_exchange";
        String routingKey = "returns.save";
        String routingErrorKey = "abc.save";
        String msg = "Hello RabbitMQ!";

        //新增return 监听器
        channel.addReturnListener(new ReturnListener() {
           @Override
           public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, AMQP.BasicProperties properties, byte[] body) throws IOException {
               System.out.println("------------handleReturn---------");
               System.out.println("replyCode: "+replyCode);
               System.out.println("replyText: "+replyText);
               System.out.println("exchange: "+exchange);
               System.out.println("routingKey: "+routingKey);
               System.out.println("properties: "+properties);
               System.out.println("body: "+new String(body));
           }
       });

        //mandatory: 如果为true,则监听器会接收到路由不可达的信息，然后进行后续处理，如果为false,那么broker端字典删除该消息
        channel.basicPublish(exchangeName,routingErrorKey,true,null,msg.getBytes());

    }
}
